package licode.unisop.authcenter.adapter.web;

import licode.unisop.authcenter.application.executor.CenterRealmExecutor;
import licode.unisop.client.inter.HasAuthority;
import licode.unisop.provider.vo.SiUser;
import licode.unisop.realm.client.dto.SiRealmDTO;
import licode.unisop.realm.client.info.SiUserSaveIn;
import licode.unisop.types.AuthConst;
import licode.unisop.unilite.result.UniReturnResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 定义统一认证平台中心领域相关接口
 *
 * @author licode
 */
@Slf4j
@UniReturnResult
@RestController
@RequestMapping("/uni-center/v1/realm")
public class CenterRealmController extends CenterBaseController {
    @Resource
    private CenterRealmExecutor centerRealmExecutor;

    /**
     * 保存领域信息
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @HasAuthority(roles= AuthConst.SYSTEM_ADMIN, permits = "realm:update")
    public void save(@RequestBody SiRealmDTO realm) {
        centerRealmExecutor.save(realm);
    }

    /**
     * 保存领域用户信息
     */
    @RequestMapping(value = "/user/save", method = RequestMethod.POST)
    @HasAuthority(roles=AuthConst.SYSTEM_ADMIN, permits = "realm:update")
    public void saveUser(@RequestBody SiUser user) {
        centerRealmExecutor.saveUser(SiUserSaveIn.builder()
                .user(user).authIn(getAuthIn()).build());
    }
}
